package com.eclipsemail.core.message;

import java.util.Collections;
import java.util.List;

import org.joda.time.LocalDateTime;

import com.google.common.collect.Lists;

public class EmailMessage {

    // TODO criar os removes da vida tb
    private List<NetAddress> from;
    private List<NetAddress> to;
    private List<NetAddress> cc;
    private List<NetAddress> cco;

    private String title;
    private String contentType; // TODO enum?
    private String content;

    private LocalDateTime receivedDate;
    private LocalDateTime sendDate;

    // private List<Attach> attachs; // FIXME

    public String getTitle() {
        return title;
    }

    public List<NetAddress> getFrom() {
        return returnEmptyOrUnmodifiable(from);
    }

    public void addFrom(NetAddress address) {
        if (this.from == null) {
            this.from = Lists.newArrayList();
        }
        this.from.add(address);
    }

    public List<NetAddress> getTo() {
        return returnEmptyOrUnmodifiable(to);
    }

    public void addTo(NetAddress address) {
        if (this.to == null) {
            this.to = Lists.newArrayList();
        }
        this.to.add(address);
    }

    public List<NetAddress> getCc() {
        return returnEmptyOrUnmodifiable(cc);
    }

    public void addCc(NetAddress address) {
        if (this.cc == null) {
            this.cc = Lists.newArrayList();
        }
        this.cc.add(address);
    }

    public void addCco(NetAddress address) {
        if (this.cco == null) {
            this.cco = Lists.newArrayList();
        }
        this.cco.add(address);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return this.content;
    }

    public LocalDateTime getReceivedDate() {
        return receivedDate;
    }

    public void setReceivedDate(LocalDateTime receivedDate) {
        this.receivedDate = receivedDate;
    }

    public LocalDateTime getSendDate() {
        return sendDate;
    }

    public void setSentDate(LocalDateTime sendDate) {
        this.sendDate = sendDate;
    }

    private <T> List<T> returnEmptyOrUnmodifiable(List<T> list) {
        if (list == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableList(list);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("EmailMessage [cc=");
        builder.append(cc);
        builder.append(", cco=");
        builder.append(cco);
        builder.append(", content=");
        builder.append(content);
        builder.append(", contentType=");
        builder.append(contentType);
        builder.append(", from=");
        builder.append(from);
        builder.append(", receivedDate=");
        builder.append(receivedDate);
        builder.append(", sendDate=");
        builder.append(sendDate);
        builder.append(", title=");
        builder.append(title);
        builder.append(", to=");
        builder.append(to);
        builder.append("]");
        return builder.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cc == null) ? 0 : cc.hashCode());
        result = prime * result + ((cco == null) ? 0 : cco.hashCode());
        result = prime * result + ((content == null) ? 0 : content.hashCode());
        result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
        result = prime * result + ((from == null) ? 0 : from.hashCode());
        result = prime * result + ((receivedDate == null) ? 0 : receivedDate.hashCode());
        result = prime * result + ((sendDate == null) ? 0 : sendDate.hashCode());
        result = prime * result + ((title == null) ? 0 : title.hashCode());
        result = prime * result + ((to == null) ? 0 : to.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        EmailMessage other = (EmailMessage) obj;
        if (cc == null) {
            if (other.cc != null) {
                return false;
            }
        } else if (!cc.equals(other.cc)) {
            return false;
        }
        if (cco == null) {
            if (other.cco != null) {
                return false;
            }
        } else if (!cco.equals(other.cco)) {
            return false;
        }
        if (content == null) {
            if (other.content != null) {
                return false;
            }
        } else if (!content.equals(other.content)) {
            return false;
        }
        if (contentType == null) {
            if (other.contentType != null) {
                return false;
            }
        } else if (!contentType.equals(other.contentType)) {
            return false;
        }
        if (from == null) {
            if (other.from != null) {
                return false;
            }
        } else if (!from.equals(other.from)) {
            return false;
        }
        if (receivedDate == null) {
            if (other.receivedDate != null) {
                return false;
            }
        } else if (!receivedDate.equals(other.receivedDate)) {
            return false;
        }
        if (sendDate == null) {
            if (other.sendDate != null) {
                return false;
            }
        } else if (!sendDate.equals(other.sendDate)) {
            return false;
        }
        if (title == null) {
            if (other.title != null) {
                return false;
            }
        } else if (!title.equals(other.title)) {
            return false;
        }
        if (to == null) {
            if (other.to != null) {
                return false;
            }
        } else if (!to.equals(other.to)) {
            return false;
        }
        return true;
    }

}
